/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.chlitina.store.modules.report.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.chlitina.store.common.config.Global;
import com.chlitina.store.common.persistence.Page;
import com.chlitina.store.common.utils.DateUtils;
import com.chlitina.store.common.web.BaseController;
import com.chlitina.store.modules.cash.entity.StoreConsumeBuyProduct;
import com.chlitina.store.modules.cash.entity.StoreConsumeDoCare;
import com.chlitina.store.modules.cash.entity.StoreConsumeNewCard;
import com.chlitina.store.modules.cash.entity.StoreConsumeRechargeCard;
import com.chlitina.store.modules.employee.model.EmployeeAccounting;
import com.chlitina.store.modules.expert.entity.CustomerSolution;
import com.chlitina.store.modules.report.model.BusinessAll;
import com.chlitina.store.modules.report.model.ReportCard;
import com.chlitina.store.modules.report.model.ReportCare;
import com.chlitina.store.modules.report.model.ReportEmployeeBonusDetail;
import com.chlitina.store.modules.report.model.ReportProduct;
import com.chlitina.store.modules.report.service.ReportService;

/**
 * 营业报表Controller
 * @author lvbo
 * @version 2013-11-29
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH+"/report/all")
public class ReportController extends BaseController {
	public static final String REPORT_PRODUCT = "p";
	public static final String REPORT_CARE = "c";
	public static final String REPORT_SOLUTION = "s";
	public static final String REPORT_BONUS = "b";
	public static final String REPORT_NEW_AND_RECHARGE = "nr";
	@Autowired
	private ReportService reportService;

	
	@RequestMapping(value = {"list", ""})
	public String list(BusinessAll businessAll, HttpServletRequest request, HttpServletResponse response, Model model) {
		String startDate = request.getParameter("startDate");
		String endDate = request.getParameter("endDate");
		if (startDate == null) {
			startDate = DateUtils.getPastDate(7);
		}
		if (endDate == null) {
			endDate = DateUtils.getDate();
		}
		BusinessAll all = reportService.findAll(startDate, endDate);
		model.addAttribute("all", all);
        model.addAttribute("startDate", startDate);
        model.addAttribute("endDate", endDate);
        return "modules/report/reportAll";
	}
	@RequestMapping(value = "detail")
	public String detail(HttpServletRequest request, HttpServletResponse response, Model model) {
		String detailId = request.getParameter("detailId");
		if (REPORT_PRODUCT.equals(detailId)) {
			return product(request, response, model);
		}
		if (REPORT_CARE.equals(detailId)) {
			return care(request, response, model);
		}
		if (REPORT_NEW_AND_RECHARGE.equals(detailId)) {
			return card(request, response, model);
		}
		if (REPORT_BONUS.equals(detailId)) {
			return bonus(request, response, model);
		}
		if (REPORT_SOLUTION.equals(detailId)) {
			return solution(request, response, model);
		}
		return "";
	}
	@RequestMapping(value = "solution")
	public String solution(HttpServletRequest request, HttpServletResponse response, Model model) {
		String startDate = request.getParameter("startDate");
		String endDate = request.getParameter("endDate");
		if (startDate == null || "".equals(startDate)) {
			startDate = DateUtils.getPastDate(7);
		}
		if (endDate == null|| "".equals(endDate)) {
			endDate = DateUtils.getDate();
		}
		Page<CustomerSolution> page = new Page<CustomerSolution>(request, response);
		List<CustomerSolution> list = reportService.findSolutionByDuration(startDate, endDate);
		page.updatePage(list);
		model.addAttribute("startDate", startDate);
		model.addAttribute("endDate", endDate);
		model.addAttribute("page", page);
		return "modules/report/reportSolution";
	}
	@RequestMapping(value = "product")
	public String product(HttpServletRequest request, HttpServletResponse response, Model model) {
		String startDate = request.getParameter("startDate");
		String endDate = request.getParameter("endDate");
		if (startDate == null || "".equals(startDate)) {
			startDate = DateUtils.getPastDate(7);
		}
		if (endDate == null|| "".equals(endDate)) {
			endDate = DateUtils.getDate();
		}
		Page<ReportProduct> page = new Page<ReportProduct>(request, response);
		List<ReportProduct> list = reportService.findProductByDuration(startDate, endDate);
		page.updatePage(list);
		model.addAttribute("startDate", startDate);
		model.addAttribute("endDate", endDate);
		model.addAttribute("page", page);
		return "modules/report/reportProduct";
	}
	
	@RequestMapping(value = "care")
	public String care(HttpServletRequest request, HttpServletResponse response, Model model) {
		String startDate = request.getParameter("startDate");
		String endDate = request.getParameter("endDate");
		if (startDate == null || "".equals(startDate)) {
			startDate = DateUtils.getPastDate(7);
		}
		if (endDate == null || "".equals(endDate)) {
			endDate = DateUtils.getDate();
		}
		Page<ReportCare> page = new Page<ReportCare>(request, response);
		List<ReportCare> list = reportService.findCareByDuration(startDate, endDate);
		page.updatePage(list);
		model.addAttribute("startDate", startDate);
		model.addAttribute("endDate", endDate);
		model.addAttribute("page", page);
		return "modules/report/reportCare";
	}
	@RequestMapping(value = "card")
	public String card(HttpServletRequest request, HttpServletResponse response, Model model) {
		String startDate = request.getParameter("startDate");
		String endDate = request.getParameter("endDate");
		if (startDate == null || "".equals(startDate)) {
			startDate = DateUtils.getPastDate(7);
		}
		if (endDate == null || "".equals(endDate)) {
			endDate = DateUtils.getDate();
		}
		Page<ReportCard> page = new Page<ReportCard>(request, response);
		List<ReportCard> list = reportService.findCardByDuration(startDate, endDate);
		page.updatePage(list);
		model.addAttribute("startDate", startDate);
		model.addAttribute("endDate", endDate);
		model.addAttribute("page", page);
		return "modules/report/reportCard";
	}
	@RequestMapping(value = "bonus")
	public String bonus(HttpServletRequest request, HttpServletResponse response, Model model) {
		String startDate = request.getParameter("startDate");
		String endDate = request.getParameter("endDate");
		if (startDate == null || "".equals(startDate)) {
			startDate = DateUtils.getPastDate(7);
		}
		if (endDate == null || "".equals(endDate)) {
			endDate = DateUtils.getDate();
		}
		Page<EmployeeAccounting> page = new Page<EmployeeAccounting>(request, response);
		List<EmployeeAccounting> list = reportService.findBonusByDuration(startDate, endDate);
		page.updatePage(list);
		model.addAttribute("startDate", startDate);
		model.addAttribute("endDate", endDate);
		model.addAttribute("page", page);
		return "modules/report/reportBonus";
	}
	@RequestMapping(value = "productDetail")
	public String productDetail(HttpServletRequest request, HttpServletResponse response, Model model) {
		String startDate = request.getParameter("startDate");
		String endDate = request.getParameter("endDate");
		String productId = request.getParameter("productId");
		Page<StoreConsumeBuyProduct> page = new Page<StoreConsumeBuyProduct>(request, response);
		List<StoreConsumeBuyProduct> list = reportService.findProductDetailByDuration(productId, startDate, endDate);
		page.updatePage(list);
		model.addAttribute("startDate", startDate);
		model.addAttribute("endDate", endDate);
		model.addAttribute("productId", productId);
		model.addAttribute("page", page);
		return "modules/report/reportProductDetail";
	}
	@RequestMapping(value = "careDetail")
	public String careDetail(HttpServletRequest request, HttpServletResponse response, Model model) {
		String startDate = request.getParameter("startDate");
		String endDate = request.getParameter("endDate");
		String careId = request.getParameter("careId");
		Page<StoreConsumeDoCare> page = new Page<StoreConsumeDoCare>(request, response);
		List<StoreConsumeDoCare> list = reportService.findCareDetailByDuration(careId, startDate, endDate);
		page.updatePage(list);
		model.addAttribute("careId", careId);
		model.addAttribute("startDate", startDate);
		model.addAttribute("endDate", endDate);
		model.addAttribute("page", page);
		return "modules/report/reportCareDetail";
	}
	@RequestMapping(value = "cardDetail")
	public String cardDetail(HttpServletRequest request, HttpServletResponse response, Model model) {
		String startDate = request.getParameter("startDate");
		String endDate = request.getParameter("endDate");
		String cardTmpId = request.getParameter("cardTmpId");
		String cardType = request.getParameter("cardType");
		String reportType = request.getParameter("reportType");
		model.addAttribute("startDate", startDate);
		model.addAttribute("endDate", endDate);
		model.addAttribute("cardTmpId", cardTmpId);
		model.addAttribute("cardType", cardType);
		model.addAttribute("reportType", reportType);
		if (ReportCard.REPORT_TYPE_NEW == Integer.parseInt(reportType)) {
			Page<StoreConsumeNewCard> page = new Page<StoreConsumeNewCard>(request, response);
			List<StoreConsumeNewCard> list = reportService.findNewCardDetailByDuration(cardTmpId,cardType, startDate, endDate);
			page.updatePage(list);
			model.addAttribute("page", page);
		} else if (ReportCard.REPORT_TYPE_RECHARGE == Integer.parseInt(reportType)) {
			Page<StoreConsumeRechargeCard> page = new Page<StoreConsumeRechargeCard>(request, response);
			List<StoreConsumeRechargeCard> list = reportService.findRechargeCardDetailByDuration(cardTmpId, cardType, startDate, endDate);
			page.updatePage(list);
			model.addAttribute("page", page);
		}
		return "modules/report/reportCardDetail";
	}
	@RequestMapping(value = "bonusDetail")
	public String bonusDetail(HttpServletRequest request, HttpServletResponse response, Model model) {
		String startDate = request.getParameter("startDate");
		String endDate = request.getParameter("endDate");
		String employeeId = request.getParameter("employeeId");
		ReportEmployeeBonusDetail detail = reportService.findBonusDetailByDuration(employeeId, startDate, endDate);
		model.addAttribute("detail", detail);
		return "modules/report/reportBonusDetail";
	}
	@RequestMapping(value = "solutionDetail")
	public String solutionDetail(HttpServletRequest request, HttpServletResponse response, Model model) {
		String solutionId = request.getParameter("solutionId");
		CustomerSolution detail = reportService.findSolutionDetailByDuration(solutionId);
		model.addAttribute("detail", detail);
		return "modules/report/reportSolutionDetail";
	}
}
